package com.example.pageexample.net;

import java.io.IOException;

import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class PictureInterceptor implements Interceptor {

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        //添加新的请求参数
        HttpUrl.Builder builder = request.url().newBuilder();
        String query = request.url().query();
        builder.removeAllQueryParameters("word");
        builder.addQueryParameter("tn","baiduimage");
        builder.addQueryParameter("ipn","r");
        builder.addQueryParameter("ct","201326592");
        builder.addQueryParameter("cl","2");
        builder.addQueryParameter("lm","-1");
        builder.addQueryParameter("st","-1");
        builder.addQueryParameter("sf","1");
        builder.addQueryParameter("fmq","");
        builder.addQueryParameter("pv","");
        builder.addQueryParameter("ic","0");
        builder.addQueryParameter("nc","1");
        builder.addQueryParameter("z","");
        builder.addQueryParameter("se","1");
        builder.addQueryParameter("showtab","0");
        builder.addQueryParameter("fb","0");
        builder.addQueryParameter("width","");
        builder.addQueryParameter("height","");
        builder.addQueryParameter("face","0");
        builder.addQueryParameter("istype","2");
        builder.addQueryParameter("ie","utf-8");
        builder.addQueryParameter("fm","index");
        builder.addQueryParameter("pos","history");
        builder.addQueryParameter(query.substring(0,query.indexOf("=")),query.substring(query.indexOf("=") , query.length()-1));
        //重构请求数据
        HttpUrl newUrl = builder.build();
        Request newQuest = request.newBuilder()
                .url(newUrl)
                .method(request.method() , request.body())
                .build();
        return chain.proceed(newQuest);
    }
}
